package com.example.menudialogtest_206shj;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    private TextView tv1, tv2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }
//创建选项菜单和子菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //准备动态加载菜单
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        initView();
        return true;
    }

    private void initView(){
        tv1 = (TextView) findViewById(R.id.textView1);
        tv2 = (TextView) findViewById(R.id.textView2);

        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "something", Toast.LENGTH_SHORT).show();
            }
        });

        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "something", Toast.LENGTH_SHORT).show();
            }
        });
    }

//相应选项菜单和子菜单的点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu1:
                tv1.setText("菜单1");
                showdialog();
                break;
            case R.id.menu2:
                tv1.setText("菜单2");
                break;
            case R.id.menu3:
                tv1.setText("菜单3");
                break;
            case R.id.menu4:
                tv1.setText("菜单4");
                break;
            case R.id.menu5:
                tv1.setText("菜单5");
                break;
            case R.id.menu6:
                tv1.setText("菜单6");
                break;
            default:
        }
        return true;
    }

    private void showdialog() {
        //动态加载布局
        final View view = getLayoutInflater().inflate(R.layout.dialoglayout, null);
        AlertDialog dialog = new AlertDialog.Builder(this)//1
                                                        .setTitle("对话框标题")//2
                                                        .setIcon(R.mipmap.ic_launcher)
                                                        .setView(view)//4自定义布局
                                                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                            @Override
                                                            public void onClick(DialogInterface dialog, int which) {
                                                                //点击确定按钮要做的
                                                                EditText et = (EditText)view.findViewById(R.id.editText);
                                                                String str = et.getText().toString();
                                                                tv1.setText(str);
                                                            }
                                                        })
                                                        .setNegativeButton("取消",null)
                                                        .create();
        dialog.show();
    }
}
